﻿#region Imports
using System;
#endregion Imports

namespace CommonUtilities.Attributes
{
    /// <summary>
    /// Allows sorting of properties against specified indices
    /// <para>(Note: No restrictions on index; properties will be assigned consecutive index, starting from 0, at runtime)</para>
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
    public sealed class SortIndexAttribute : Attribute
    {
        #region Declaration
        /// <summary>
        /// Default value for SortIndex property
        /// </summary>
        private const int defaultSortIndex = -1;

        /// <summary>
        /// Default property name
        /// </summary>
        public static readonly string DefaultPropertyName = "SortIndex";

        /// <summary>
        /// Specifies the default instance for the <code>CommonUtilities.Attributes.SortIndexAttribute</code>
        /// </summary>
        public static readonly SortIndexAttribute Default = new SortIndexAttribute() { SortIndex = defaultSortIndex };

        /// <summary>
        /// Specifies the default value for the <code>CommonUtilities.Attributes.SortIndexAttribute.SortIndex</code> when property not attributed to
        /// </summary>
        public static readonly int DefaultValueOnAbsent = Default.SortIndex;
        #endregion

        #region Properties
        /// <summary>
        /// Specifies if property is sortable
        /// </summary>
        public int SortIndex { get; private set; }
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new default instance of <code>CommonUtilities.Attributes.SortIndexAttribute</code>
        /// </summary>
        internal SortIndexAttribute()
            : base()
        {
            SortIndex = defaultSortIndex;
        }

        /// <summary>
        /// Initializes a new instance of <code>CommonUtilities.Attributes.SortIndexAttribute</code> with the specified sort index
        /// <para>(Hint: mark properties with positive indices; mark inherited ones with negatives; properties will be sorted against index before loading)</para>
        /// </summary>
        public SortIndexAttribute(int sortIndex)
            : this()
        {
            SortIndex = sortIndex;
        }
        #endregion

        #region Methods
        public override bool Equals(object obj)
        {
            return this.SortIndex == ((SortIndexAttribute)obj).SortIndex;
        }

        /// <summary>
        /// Determines if this attribute is the default instance
        /// </summary>
        /// <returns>TRUE if the attribute is the default instance for this attribute class;
        /// <para>otherwise, FALSE</para></returns>
        public override bool IsDefaultAttribute()
        {
            return this.SortIndex == Default.SortIndex;
        }
        #endregion
    }
}
